﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ByteSite.Common;
using System.Linq.Expressions;

namespace ByteSite.Web.UI
{
    public partial class BasePage : System.Web.UI.Page
    {
        #region 列表标签======================================
        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int top, string strWhere)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list.AddRange(new BLL.article().ArticleList(channelName, top, strWhere, "sort_id asc,add_time desc"));
            }
            return list;
        }
        public List<Entitys.article> GetArticleFullList(string channelName, int top, string strWhere)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list.AddRange(new BLL.article().ArticleFullList(channelName, top, strWhere, "sort_id asc,add_time desc"));
            }
            return list;
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int categoryId, int top, string strWhere)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list.AddRange(new BLL.article().ArticleList(channelName, categoryId, top, strWhere, "sort_id asc,add_time desc"));
            }
            
            return list;
        }
        public List<Entitys.article> GetArticleFullList(string channelName, int categoryId, int top, string strWhere)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list.AddRange(new BLL.article().ArticleFullList(channelName, categoryId, top, strWhere, "sort_id asc,add_time desc"));
            }
            return list;
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderBy">排序</param>
        /// <returns>DataTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int categoryId, int top, string strWhere, string orderBy)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleList(channelName, categoryId, top, strWhere, orderBy);
            }
            return list;
        }

        public List<Entitys.article> GetArticleFullList(string channelName, int categoryId, int top, string strWhere, string orderBy)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleFullList(channelName, categoryId, top, strWhere, orderBy);
            }
            return list;
        }

        /// <summary>
        /// 文章分页列表(自定义页面大小)
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="totalCount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string orderBy, out int totalCount)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleList(channelName, categoryId, pageSize, pageIndex, strWhere, orderBy, out totalCount);
            }
            else
            {
                totalCount = 0;
            }
            return list;
        }
        public List<Entitys.article> GetArticleFullList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string orderBy, out int totalCount)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleFullList(channelName, categoryId, pageSize, pageIndex, strWhere, orderBy, out totalCount);
            }
            else
            {
                totalCount = 0;
            }
            return list;
        }

        /// <summary>
        /// 文章分页列表(自动页面大小)
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="key">URL配置名称</param>
        /// <param name="parms">传输参数</param>
        /// <returns>DataTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, out int totalCount, out string pageList, string key, params object[] parms)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleList(channelName, categoryId, pageSize, pageIndex, strWhere, "sort_id asc,add_time desc", out totalCount);
                pageList = Utils.OutPageList(pageSize, pageIndex, totalCount, LinkUrl(key, parms), 8);
            }
            else
            {
                totalCount = 0;
                pageList = "";
            }
            return list;
        }
        public List<Entitys.article> GetArticleFullList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, out int totalCount, out string pageList, string key, params object[] parms)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleFullList(channelName, categoryId, pageSize, pageIndex, strWhere, "sort_id asc,add_time desc", out totalCount);
                pageList = Utils.OutPageList(pageSize, pageIndex, totalCount, LinkUrl(key, parms), 8);
            }
            else
            {
                totalCount = 0;
                pageList = "";
            }
            return list;
        }
        public Entitys.ArticleList GetArticleList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string key, string parms)
        {
            string[] arrayParms = parms.Split('|');

            Entitys.ArticleList data = new Entitys.ArticleList();
            data.page = pageIndex;
            data.size = pageSize;
            int totalCount = 0;
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                data.list = new BLL.article().ArticleList(channelName, categoryId, pageSize, pageIndex, strWhere, "sort_id asc,add_time desc", out totalCount);
                data.totalCount = totalCount;
                data.pageList = Utils.OutPageList(pageSize, pageIndex, data.totalCount, LinkUrl(key, arrayParms), 8);

            }
            else
            {
                data.totalCount = 0;
                data.pageList = "";
            }
            return data;
        }
        public Entitys.ArticleList GetArticleFullList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string key, string parms)
        {
            string[] arrayParms = parms.Split('|');

            Entitys.ArticleList data = new Entitys.ArticleList();
            data.page = pageIndex;
            data.size = pageSize;
            int totalCount = 0;
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                data.list = new BLL.article().ArticleFullList(channelName, categoryId, pageSize, pageIndex, strWhere, "sort_id asc,add_time desc", out totalCount);
                data.totalCount = totalCount;
                data.pageList = Utils.OutPageList(pageSize, pageIndex, data.totalCount, LinkUrl(key, arrayParms), 8);

            }
            else
            {
                data.totalCount = 0;
                data.pageList = "";
            }
            return data;
        }

        /// <summary>
        /// 文章分页列表(可排序)
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="key">URL配置名称</param>
        /// <param name="parms">传输参数</param>
        /// <returns>DataTable</returns>
        public List<Entitys.article> GetArticleList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string orderBy, out int totalCount, out string pageList, string key, params object[] parms)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleList(channelName, categoryId, pageSize, pageIndex, strWhere, orderBy, out totalCount);
                pageList = Utils.OutPageList(pageSize, pageIndex, totalCount, LinkUrl(key, parms), 8);
            }
            else
            {
                totalCount = 0;
                pageList = "";
            }
            return list;
        }
        public List<Entitys.article> GetArticleFullList(string channelName, int categoryId, int pageSize, int pageIndex, string strWhere, string orderBy, out int totalCount, out string pageList, string key, params object[] parms)
        {
            List<Entitys.article> list = new List<Entitys.article>();
            if (!string.IsNullOrEmpty(channelName))
            {
                list = new BLL.article().ArticleFullList(channelName, categoryId, pageSize, pageIndex, strWhere, orderBy, out totalCount);
                pageList = Utils.OutPageList(pageSize, pageIndex, totalCount, LinkUrl(key, parms), 8);
            }
            else
            {
                totalCount = 0;
                pageList = "";
            }
            return list;
        }

        /// <summary>
        /// 文章Tags标签列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public List<Entitys.dt_article_tags> GetArticleTags(int top, string strwhere)
        {
            return new BLL.article_tags().GetList(top, strwhere, "count desc");
        }
        #endregion

        #region 内容标签======================================
        /// <summary>
        /// 根据调用标识取得内容
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="callIndex">调用别名</param>
        /// <returns>String</returns>
        public string GetArticleContent(string channelName, string callIndex)
        {
            if (!string.IsNullOrEmpty(channelName) && !string.IsNullOrEmpty(callIndex))
            {
                return new BLL.article().GetContent(channelName, callIndex);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取对应的图片路径
        /// </summary>
        /// <param name="channelId">频道ID</param>
        /// <param name="articleId">信息ID</param>
        /// <returns>String</returns>
        public string GetArticleImgUrl(int channelId, int articleId)
        {
            if (channelId > 0)
            {
                return new BLL.article().GetImgUrl(channelId, articleId);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取扩展字段的值
        /// </summary>
        /// <param name="channelId">频道ID</param>
        /// <param name="articleId">内容ID</param>
        /// <param name="fieldName">扩展字段名</param>
        /// <returns>String</returns>
        public string GetArticleField(int channelId, int articleId, string fieldName)
        {
            Entitys.article model = new BLL.article().GetModel(channelId, articleId);
            if (model != null && model.fields.ContainsKey(fieldName))
            {
                return Utils.ObjectToStr(model.fields[fieldName]);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取扩展字段的值
        /// </summary>
        /// <param name="channelId">频道ID</param>
        /// <param name="callIndex">调用别名</param>
        /// <param name="fieldName">扩展字段名</param>
        /// <returns>String</returns>
        public string GetArticleField(int channelId, string callIndex, string fieldName)
        {
            if (string.IsNullOrEmpty(callIndex))
                return string.Empty;
            BLL.article bll = new BLL.article();
            if (!bll.Exists(channelId, callIndex))
            {
                return string.Empty;
            }
            Entitys.article model = bll.GetModel(channelId, callIndex);
            if (model != null && model.fields.ContainsKey(fieldName))
            {
                return Utils.ObjectToStr(model.fields[fieldName]);
            }
            return string.Empty;
        }
        #endregion

    }
}
